//
//  MHMomentProfileViewModel.m
//  MHDevelopExample
//
//Created by acgera on 2018/4/9.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHMomentProfileViewModel.h"
#import "MHRecentMessageViewModel.h"
#import "MHIMMessageTransferHelper.h"
#import "MHMessageNotification.h"

@interface MHMomentProfileViewModel ()
/// 用户模型
@property (nonatomic, readwrite, strong) MHUser *user;

/// 昵称
@property (nonatomic, readwrite, copy) NSAttributedString *screenNameAttr;


@end

@implementation MHMomentProfileViewModel


-(instancetype)initWithServices:(id<MHViewModelServices>)services params:(NSDictionary *)params
{
    self = [super initWithServices:services params:params];
    if (self) {
        self.user = params[MHViewModelUtilKey];
        
        @weakify(self);
        [RACObserve([MHIMMessageTransferHelper sharedInstance],unReadRecentMessage) subscribeNext:^(NSNumber *unreadNumber) {
            @strongify(self);
            self.unread = [unreadNumber intValue];
            self.unreadUser = [[MHUser alloc] init];
            MHMessageNotification *message = [[[MHIMMessageTransferHelper sharedInstance] fetchChatIMMessageWithName:NSStringFromClass([MHMessageNotification class])] lastObject];
            if (message.fullOprtUserFace.length > 0) {
                self.unreadUser.avatarLarge = [NSURL URLWithString:message.fullOprtUserFace];
            }
        }];
       
        
        /// 设置昵称的外部阴影
        NSMutableAttributedString *screenNameAttr = [[NSMutableAttributedString alloc] initWithString:@""];
        screenNameAttr.yy_alignment = NSTextAlignmentRight;
        screenNameAttr.yy_font = MHMediumFont(20.0f);
        screenNameAttr.yy_color = [UIColor whiteColor];
        YYTextShadow *textShadow = [YYTextShadow new];
        textShadow.color = MHColorFromHexString(@"#212227");
        textShadow.offset = CGSizeMake(1, 2);
        textShadow.radius = 2;
        screenNameAttr.yy_textShadow = textShadow;
        self.screenNameAttr = screenNameAttr;
        self.redirectToMessageCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            MHRecentMessageViewModel *viewModel = [[MHRecentMessageViewModel alloc] initWithServices:self.services params:@{@"isTemp":@(true)}];
//            viewModel.userId = [MHHTTPService sharedInstance].currentUser.userId;
            [self.services pushViewModel:viewModel animated:YES];
            return [RACSignal empty];
        }];
    }
    return self;
}

- (instancetype)initWithUser:(MHUser *)user
{
    if (self = [super init]) {
        
        /// user
        self.user = user;
        _unread = [MHIMMessageTransferHelper sharedInstance].unReadRecentMessage;
        /// 假数据
        MHMessageNotification *message = [[[MHIMMessageTransferHelper sharedInstance] fetchChatIMMessageWithName:NSStringFromClass([MHMessageNotification class])] lastObject];
        if (message.fullOprtUserFace.length > 0) {
            _unreadUser.avatarLarge = [NSURL URLWithString:message.fullOprtUserFace];
        }
        
        /// 设置昵称的外部阴影
        NSMutableAttributedString *screenNameAttr = [[NSMutableAttributedString alloc] initWithString:@""];
        screenNameAttr.yy_alignment = NSTextAlignmentRight;
        screenNameAttr.yy_font = MHMediumFont(20.0f);
        screenNameAttr.yy_color = [UIColor whiteColor];
        YYTextShadow *textShadow = [YYTextShadow new];
        textShadow.color = MHColorFromHexString(@"#212227");
        textShadow.offset = CGSizeMake(1, 2);
        textShadow.radius = 2;
        screenNameAttr.yy_textShadow = textShadow;
        self.screenNameAttr = screenNameAttr;
        
    }
    return self;
}


/// 如果高度是动态的
- (CGFloat)height
{
    if (self.unread>0) {
        ///
        return (101.0f);
    }
    return (30);
}

- (NSString *)unreadTips
{
    if (self.unread>0) {
        return [NSString stringWithFormat:@"%zd条新消息",self.unread];
    }
    return @"";
}


@end
